package com.trkj.usercenter;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.trkj.base.BaseDataRefreshFragment;
import com.trkj.base.Constants;
import com.trkj.taoruiyun.R;

/**
 * Created by LiZanhong on 2015/06/24.
 */
public class NoticeFragment extends BaseDataRefreshFragment{

    @Override
    public String getUrl() {
        return Constants.ApiUrl.NOTICE_LIST;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //先定义Adapter
        if(getAdapter() == null) {
            setAdapter(new NoticeAdapter());
        }
        View root = super.onCreateView(inflater, container, savedInstanceState);
        return root;
    }

    class NoticeAdapter extends BaseAdapter{
        @Override
        public int getCount() {
            return getData().size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final ViewHolder viewHolder;

            if(convertView == null){
                viewHolder = new ViewHolder();
                convertView = LayoutInflater.from(getActivity()).inflate(
                        R.layout.activity_notice_item, null);
                viewHolder.noticeContent = (TextView)convertView.findViewById(
                        R.id.notice_content);
                viewHolder.top = (TextView)convertView.findViewById(
                        R.id.top);
                viewHolder.noticeTime = (TextView)convertView.findViewById(
                        R.id.notice_time);
                convertView.setTag(viewHolder);
            }else{
                viewHolder = (ViewHolder) convertView.getTag();
            }

            JSONArray data = getData();
            if(data.getJSONObject(position).getBoolean("is_top")){
                viewHolder.top.setVisibility(View.VISIBLE);
                Constants.Font.setAwesomeFont(getActivity(), viewHolder.top);
                //图标字体要保存在string资源中才能显示图案
                viewHolder.top.setText(Constants.ResString.getString(
                        getActivity(), R.string.fa_angle_double_up));
            }else{
                viewHolder.top.setVisibility(View.GONE);
            }

            viewHolder.noticeContent.setText(data.getJSONObject(position)
                    .getString("notice_content"));
            viewHolder.noticeTime.setText(
                    data.getJSONObject(position)
                            .getString("notice_time")
                            .substring(0, 10));

            return convertView;
        }

        class ViewHolder{
            TextView noticeContent;
            TextView top;
            TextView noticeTime;
        }
    }

}
